package com.fastloan.mgt.invest.controller.subject;

import com.fastloan.mgt.invest.model.InvestRegion;
import com.fastloan.mgt.invest.model.InvestSubject;
import com.fastloan.mgt.invest.model.RegionSubject;
import com.fastloan.mgt.invest.service.InvestRegionService;
import com.fastloan.mgt.invest.service.InvestSubjectService;
import com.fastloan.mgt.invest.service.InvestTempSubjectService;
import com.fastloan.mgt.invest.service.RegionSubjectService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目管理
 *
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:39
 */
@RestController
@RequestMapping("/api/invest/subject")
public class InvestSubjectController extends BaseController<InvestSubject> {

    @Autowired
    private InvestSubjectService investSubjectService;

    @Autowired
    private InvestTempSubjectService investTempSubjectService;

    @Autowired
    private InvestRegionService investRegionService;

    @Autowired
    private CmuOrgService orgService;

    @Autowired
    private RegionSubjectService regionSubjectService;

    /**
     * 项目分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList() {
        Map<String, Object> map = new HashMap<>();
        return dataPage("investSubjectPageList", map);
    }

    /**
     * 项目所有问卷列表
     *
     * @param subjectId 项目ID
     * @return
     */
    @RequestMapping("/pageList/{subjectId}")
    public ResultTo pageListForCus(@PathVariable("subjectId") String subjectId) {
        Map<String, Object> map = new HashMap<>();
        map.put("subjectId", subjectId);
        return dataPage("forCustomerPageList", map);
    }


    /**
     * 根据ID获取项目
     *
     * @param subjectId 项目ID
     * @return
     */
    @GetMapping
    public ResultTo select(@RequestParam(value = "subjectId", required = false) String subjectId) {
        Map<String, Object> map = new HashMap<>();
        Example example = new Example(InvestRegion.class);
        example.createCriteria().andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andNotEqualTo("state", 0);
        map.put("region", investRegionService.selectByExample(example));
        map.put("orgTree", orgService.selectSonByTop(ManagerKit.getTopOrgId(), true, true));
        if (subjectId != null) {
            InvestSubject investSubject = investSubjectService.selectByPrimaryKey(subjectId);
            if (investSubject.getState() == 2) {
                return new ResultTo(600, "该数据已被删除");
            }
            RegionSubject regionSubject = new RegionSubject();
            regionSubject.setSubjectId(subjectId);
            List<RegionSubject> regionSubjects = regionSubjectService.select(regionSubject);
            map.put("regionSubjects", regionSubjects);
            map.put("investSubject", investSubject);
            map.put("temp", investTempSubjectService.selectBySubId(subjectId));
        }
        return new ResultTo().setData(map);
    }

    /**
     * 获取项目列表
     *
     * @return
     */
    @GetMapping("/all")
    public ResultTo selectAll() {
        Example example = new Example(InvestSubject.class);
        example.createCriteria().andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andNotEqualTo("state", 2);
        return new ResultTo().setData(investSubjectService.selectByExample(example));
    }


    /**
     * 增加项目
     *
     * @param investSubject 项目bean
     * @param tempIds       模版集合
     * @param regionIds     区域Id集合
     * @return
     */
    @PostMapping
    public ResultTo insert(InvestSubject investSubject,
                           @RequestParam("tempIds") String tempIds,
                           @RequestParam("regionIds") String regionIds) {
        if (StringUtils.isBlank(tempIds) || StringUtils.isBlank(regionIds)) {
            return new ResultTo(ResultEnum.PARAM_ERROR);
        }
        investSubjectService.insertForTempIds(investSubject, tempIds, regionIds);
        return new ResultTo();
    }

    /**
     * 更新项目
     *
     * @param investSubject 项目bean
     * @param regionIds     区域Id集合
     * @return
     */
    @PutMapping
    public ResultTo update(InvestSubject investSubject,
                           @RequestParam(value = "regionIds", required = false) String regionIds) {
        investSubjectService.update(investSubject, regionIds);
        return new ResultTo();
    }

    /**
     * 项目名称验重
     *
     * @param name 　模版名称
     * @return
     */
    @GetMapping(params = "name")
    public ResultTo haveName(@RequestParam("name") String name) {
        if (investSubjectService.haveName(name, ManagerKit.getTopOrgId().toString()).size() > 0) {
            return new ResultTo().setData(false);
        } else {
            return new ResultTo().setData(true);
        }
    }
}
